// pages/intelligenttravel/intelligenttravel.js

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowFrom1: true,//（旅游目的）子项目-展开或收起
    isShowFrom2: false,
    isShowFrom3: false,
    isShowFrom4: false,

    isShowFrom1_tj: true,//(推荐景点)子项目-展开或收起
    isShowFrom2_tj: false,
    isShowFrom3_tj: false,
    isShowFrom4_tj: false,
    yswsList: [],//旅游目的-游山玩水
    msxcList:[],//美食小吃
    gwxpList:[],//购物血拼
    xxylList:[],//休闲娱乐

    stepIndex:1,//当前执行的步骤
    step1:true,//主项目-展开或收起
    step2:false,
    step3: false,
    show_step2:false,//默认是隐藏，执行到该步骤显示
    show_step3: false,
    tjjdList: [{  //推荐景点-名胜古迹
      name: '东湖公园',
      id: 1,
      isclick: false
    }],
    tsmsList:[],//特色美食
    gwttList:[],//购物天堂
    xxcsList:[],//休闲场所
    
    step2Data:{},//行程计划数据
    xf_array:[],//消费模式
    index_xf: 0,
    tj_array:[],//景区推荐优先级
    index_tj: 0,
    sj_array:[],//行程松紧状态
    index_sj: 0,
    jd_book_array:[],//是否已预定酒店
    index_jd: 0,
    btn_text:'下一步',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getStepsData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  getStepsData:function (parames) {
    var that = this;
    app.apiPost(app.apiList.travelPlan,{

    },function(res){
      console.log('行程预约步骤',res);
      var result = res.result;

      /************step1  data***************/
      var step1_list = result.step1_list;
      for (var i in step1_list){

        var ary = step1_list[i];

        step1_list[i] = ary.map(e => {
          return{
            name: e.name,
            id: e.id,
            isclick: false
          }
        })
      }

      that.setData({
        yswsList: step1_list['yswsList'],
        msxcList: step1_list['msxcList'],
        gwxpList: step1_list['gwxpList'],
        xxylList: step1_list['xxylList']
      })

      /************step2  data***************/
      var step2_list = result.step2_list;
      
      var stepAry = [];
      for (var i in step2_list){

        var mode = step2_list[i];
        var ary = [];
        mode.map((track)=>{
          ary.push(track.type);
        })
        stepAry.push(ary);
      }
     
      console.log(stepAry);

      that.setData({
        step2Data: step2_list,
        xf_array: stepAry[0],
        tj_array: stepAry[1],
        sj_array: stepAry[2],
        jd_book_array: stepAry[3]
      })

      /************step3  data***************/
      var step3_list = result.step3_list;
      for (var i in step3_list) {

        var ary = step3_list[i];

        step3_list[i] = ary.map(e => {
          return {
            name: e.name,
            id: e.id,
            isclick: false
          }
        })
      }

      that.setData({
        tjjdList: step3_list['tjjdList'],
        tsmsList: step3_list['tsmsList'],
        gwttList: step3_list['gwttList'],
        xxcsList: step3_list['xxcsList']
      })


    },'加载中..')
  },
  showFrom(e) {
    var param = e.target.dataset.param;
    console.log('折叠面板');
    this.setData({
      isShowFrom1: param == 1 ? (this.data.isShowFrom1 ? false : true) : false,
      isShowFrom2: param == 2 ? (this.data.isShowFrom2 ? false : true) : false,
      isShowFrom3: param == 3 ? (this.data.isShowFrom3 ? false : true) : false,
      isShowFrom4: param == 4 ? (this.data.isShowFrom4 ? false : true) : false
    });
  },
  showFrom_tj(e) {
    var param = e.target.dataset.param;
    console.log('折叠面板');
    this.setData({
      isShowFrom1_tj: param == 1 ? (this.data.isShowFrom1_tj ? false : true) : false,
      isShowFrom2_tj: param == 2 ? (this.data.isShowFrom2_tj ? false : true) : false,
      isShowFrom3_tj: param == 3 ? (this.data.isShowFrom3_tj ? false : true) : false,
      isShowFrom4_tj: param == 4 ? (this.data.isShowFrom4_tj ? false : true) : false
    });
  },
  //旅游目的
  positionDetailTap: function (e) {
    //支持复选
    var that = this;
    console.log(e.target.dataset.id);
    var index = e.currentTarget.dataset.index;
    var types = e.currentTarget.dataset.type;

    var select = types+"List[" + index + "].isclick";//先用一个变量，用字符串拼接起来

    var cur_ary;
    switch (types){
      case 'ysws':
        cur_ary = this.data.yswsList;
      break;
      case 'msxc':
        cur_ary = this.data.msxcList;
      break;
      case 'gwxp':
        cur_ary = this.data.gwxpList;
      break;
      case 'xxyl':
        cur_ary = this.data.xxylList;
      break;
    }

    if (cur_ary[index].isclick == true) {
      that.setData({
        [select]: false
      })
    } else {
      that.setData({
        [select]: true
      })
    }
  },
  //推荐景点
  positionTuiJianTap: function (e){
    var that = this;
   
    var index = e.currentTarget.dataset.index;
    var types = e.currentTarget.dataset.type;

    var select = types+"List[" + index + "].isclick";//先用一个变量，字符串拼接起来

    var cur_ary;
    switch (types) {
      case 'tjjd':
        cur_ary = this.data.tjjdList;
        break;
      case 'tsms':
        cur_ary = this.data.tsmsList;
        break;
      case 'gwtt':
        cur_ary = this.data.gwttList;
        break;
      case 'xxcs':
        cur_ary = this.data.xxcsList;
        break;
    }
    if (cur_ary[index].isclick == true) {
      that.setData({
        [select]: false
      })
    } else {
      that.setData({
        [select]: true
      })
    }
  },
  nextStep: function (e){
    console.log('下一步', this.data.stepIndex);
    var i = this.data.stepIndex;
    i = i+1;
    
    var that = this;

    var last_step = "step"+ (i - 1);
    var current_step = "step" + i;
    that.setData({
      stepIndex:i,
      [last_step]:false,
      [current_step]:true
    })
    
    switch(i){
      case 2:
        that.setData({
          show_step2: true,
        })
        break;
      case 3:
        that.setData({
          show_step3: true,
        })
        break; 
      case 4:
        console.log('提交---行程数据');
        break;   
      default:    
    }
    if (i > 3) {
      //传递参数到下个界面
      wx.navigateTo({
        url: '../scheduling/scheduling?id='+ 3,
      })

      return;
    }
  },
  edit:function(e){
    var i = e.target.dataset.step;
    var step = "step" + i;
    i = parseInt(i);
    this.setData({
      step1: i == 1 ? (this.data.step1 ? false : true) : false,
      step2: i == 2 ? (this.data.step2 ? false : true) : false,
      step3: i == 3 ? (this.data.step3 ? false : true) : false,
      [step]:true,
      stepIndex:i
    })

  },
  bindPickerChange:function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value, e.currentTarget.dataset.type);

    var types = e.currentTarget.dataset.type;

    var select = "index_" + types;
    this.setData({
      [select]: e.detail.value
    })
 
  },  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})